<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" label-width="100px" size="small" :rules="rules">
      <el-form-item label="码值:" prop="dictNo">
        <el-input v-model="dataForm.dictNo" placeholder="请输入码值" maxlength="20" clearable></el-input>
      </el-form-item>
      <el-form-item label="码值名称:" prop="dictName">
        <el-input v-model="dataForm.dictName" placeholder="请输入码值名称" maxlength="10" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">
          <fa-icon class="fa-check" />保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { CONSTANT_OBJ } from '@/utils/constant'
import { common } from '@/utils/common'
import api from '../api'
export default {
  props: {
    editData: Object,
    parentId: String
  },
  data () {
    return {
      dataForm: {
        parentId: '',
        dictNo: '',
        dictName: ''
      },
      rules: {
        dictNo: [{ required: true, validator: this.rulesDictNo, trigger: 'blur' }],
        dictName: [{ required: true, message: '请输入码值名称', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    console.log(this.parentId, '参数')
    if (this.editData && this.editData.id) {
      common.listAssign(this.dataForm, this.editData)
    }
  },
  methods: {
    submit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.parentId = this.parentId
          console.log(this.dataForm, '参数提交')
          api.addChildDict(this.dataForm).then(res => {
            const { responseCode, responseMsg: message } = res
            if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
              this.$message({
                message,
                type: 'success'
              })
              this.$emit('uppdataSuccess', this.dataForm)
            }
          })
        }
      })
    },
    // 新增
    addDict () {
      api.postAddDictClidData(this.dataForm).then(res => {
        const { responseCode, responseMsg: message } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message,
            type: 'success'
          })
          this.$emit('uppdataSuccess', this.dataForm)
        }
      })
    },
    // 编辑
    editDict () {
      api.postEditDictClidData(this.dataForm).then(res => {
        const { responseCode, responseMsg: message } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message,
            type: 'success'
          })
          this.$emit('uppdataSuccess', this.dataForm)
        }
      })
    },
    rulesDictNo (rule, value, callback) {
      const reg = /[\u4e00-\u9fa5]/gm
      if (!value) {
        return callback(new Error('请输入参数码值'))
      } else if (value.length > 20) {
        return callback(new Error('码值不可超过20个字符'))
      } else if (reg.test(value)) {
        return callback(new Error('码值只可输入数字、字母、特殊符号'))
      }
      return callback()
    }
  }
}
</script>
